package cn.good.yan.service.t1.myLock;

import com.sun.corba.se.impl.presentation.rmi.IDLTypesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.UUID;
import java.util.concurrent.locks.Lock;

/**
 * 锁的工厂
 *      如果使用Redis锁，激活Redis的分布式锁
 *      ...
 * @author shengren.yan
 * @create 2023-12-04
 */
@Component
public class DistributedLockFactory {

    @Autowired
    private RedisTemplate redisTemplate;
    private String lockName;
    private String uuidValue;

    public DistributedLockFactory() {
        this.uuidValue = UUID.randomUUID().toString();
    }

    // 工厂方法
    public Lock getDistributedLock(String lockType) {
        if (lockType == null) {
            return null;
        }
        if (lockType.equalsIgnoreCase("REDIS")) {
            this.lockName = "xxRedisLock";
            return new RedisDistributedLock(redisTemplate, lockName, uuidValue);
        }else if (lockType.equalsIgnoreCase("ZOOKEEPER")) {
            this.lockName = "xxZookeeperLock";
            // TODO zoookeeper 版本的分布式锁
            return null;
        }else if (lockType.equalsIgnoreCase("MYSQL")){
            this.lockName = "xxMysqlLock";
            // TODO MYSQL 版本的分布式锁
            return null;
        }
        return null;
    }

}
